package main

import (
	"github.com/streadway/amqp"
	"gitee.com/hlwqds/rabitmqTest/log"
	"os"
	"strings"
)

func bodyFrom(args []string) string{
	var s string
	if len(args) < 3 || args[2] == "" {
		s = "hello"
	}else{
		s = strings.Join(args[2:], " ")
	}
	return s
}

func severityFrom(args []string) string {
	var s string
	if (len(args) < 2) || os.Args[1] == "" {
		s = "info"
	} else {
		s = os.Args[1]
	}
	return s
}

func main(){
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	log.FailOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	//创建channel
	ch, err := conn.Channel()
	log.FailOnError(err, "Failed to open a channel")
	defer ch.Close()

	err = ch.ExchangeDeclare(
		"logsFilter",
		"direct",
		true,
		false,
		false,
		false,
		nil)
	log.FailOnError(err, "Failed to declare a exchange")

	body := bodyFrom(os.Args)
	err = ch.Publish(
		"logsFilter",
		severityFrom(os.Args),
		false,
		false,
		amqp.Publishing{
			//消息持久化
			DeliveryMode: amqp.Persistent,
			ContentType: "text/plain",
			Body: []byte(body),
		})
	log.FailOnError(err, "Failed to publish a message")
}